package com.wft;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Solution1021 {
    public static void main(String[] args) {
        String s = "(()())(())";
        String s1 = "(()())(())(()(()))";
        String s2 = "()()";
        System.out.println(removeOuterParentheses(s2));

    }

    public static String removeOuterParentheses(String S) {
        List<Integer> indexList = new ArrayList<>();
        int num = 0;
        for (int i = 0; i < S.length(); i++) {
            if (S.charAt(i) == '(') {
                num++;
            } else {
                num--;
            }
            if (num == 0) {
                indexList.add(i);
            }
        }
        System.out.println(Arrays.toString(indexList.toArray()));
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < indexList.size(); i++) {
            if (i == 0) {
                if (indexList.get(i) != 1) {
                    sb.append(S.substring(1, indexList.get(i)));
                }
            } else {
                if (indexList.get(i - 1) + 1 != indexList.get(i)) {
                    sb.append(S.substring(indexList.get(i - 1) + 2, indexList.get(i)));
                    System.out.println(S.substring(indexList.get(i - 1) + 2, indexList.get(i)));
                }
            }
        }
        return sb.toString();
    }
}
